//
//  GKZYBatchQueryViewController.m
//  GKZYAid
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYBatchQueryViewController.h"
#import "MyDefine.h"
#import "GKZYBatchPopView.h"
#import "GKZYBatchQueryCell.h"
#import "GKZYBatchTableView.h"
#import <AFNetworking.h>
#import "Urls.h"
#import "GKZYBatchQueryModel.h"
#import "GKZYBatchQueryBarButtonItemView.h"
#import "GKZYSortLeftBarButtonItemView.h"
#import "MBProgressHUD.h"

@interface GKZYBatchQueryViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    MBProgressHUD *HUD;
}

//下拉省份界面
@property(nonatomic,strong)GKZYBatchPopView *popView;
//批次页面
@property(nonatomic,strong)GKZYBatchTableView *batchView;
//省份数组
@property(nonatomic,strong)NSArray *provinceArray;
//理科批次数据数组
@property(nonatomic,strong)NSMutableArray *scienceArray;
//文科批次数据数组
@property(nonatomic,strong)NSMutableArray *artsArray;

//自定义右barbuttonItem 的view
@property(nonatomic,strong)GKZYBatchQueryBarButtonItemView *barButton;

//自定义左barbuttonItem 的view
@property(nonatomic,strong)GKZYSortLeftBarButtonItemView *leftBarButton;

@end

@implementation GKZYBatchQueryViewController

-(void)loadView{

    [super loadView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.batchView = [[GKZYBatchTableView alloc]initWithFrame:CGRectMake(0, 0, VCWIDTH, VCHEIGHT)];
    self.batchView.delegate = self;
    self.batchView.dataSource = self;
    self.batchView.bounces  = NO;
    self.batchView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.batchView];
   
    self.popView = [[GKZYBatchPopView alloc]initWithFrame:CGRectMake(0, -VCHEIGHT/2, VCWIDTH, VCHEIGHT/2) style:(UITableViewStyleGrouped)];
    self.popView.delegate = self;
    self.popView.dataSource = self;
    self.popView.bounces = NO;
    [self.view addSubview:self.popView];
    self.provinceArray = @[@"北京市",@"天津市",@"上海市",@"重庆市",@"河北省",@"河南省",@"云南省",@"辽宁省",@"黑龙江省",@"湖南省",@"安徽省",@"山东省",@"新疆维吾尔自治区",@"江苏省",@"浙江省",@"江西省",@"湖北省",@"广西壮族自治区",@"甘肃省",@"山西省",@"内蒙古自治区",@"陕西省",@"吉林省",@"福建省",@"贵州省",@"广东省",@"青海省",@"西藏自治区",@"四川省",@"宁夏回族自治区",@"海南省"];
}

//懒加载
-(NSMutableArray *)scienceArray{

    if (_scienceArray == nil) {
        _scienceArray = [[NSMutableArray alloc]init];
    }
    return _scienceArray;
}

-(NSMutableArray *)artsArray{

    if (_artsArray == nil) {
        _artsArray = [[NSMutableArray alloc]init];
    }
    return _artsArray;
}

-(void)viewWillAppear:(BOOL)animated{

    
    [UIView beginAnimations:@"move" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.5];
    if (self.popView.DropDown) {
        self.popView.frame = CGRectMake(0, 64, VCWIDTH, VCHEIGHT/2);
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.batchView cellForRowAtIndexPath:index].backgroundColor = [UIColor grayColor];
        self.barButton.direction.transform = CGAffineTransformMakeRotation(M_PI*1);
    }else{
        self.popView.frame = CGRectMake(0, -VCHEIGHT/2, VCWIDTH, VCHEIGHT/2);
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.batchView cellForRowAtIndexPath:index].backgroundColor = [UIColor whiteColor];
         self.barButton.direction.transform = CGAffineTransformMakeRotation(0);
    }
    
    [UIView commitAnimations];

}

//请求数据
-(void)makeData{

    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.dimBackground = YES;
    HUD.labelText = @"请稍等";
    [HUD showAnimated:YES whileExecutingBlock:^{
        [self requestTheData];
    } completionBlock:^{
        
        [HUD removeFromSuperview];
        HUD = nil;
    }];
}

-(void)requestTheData{
    
    NSString *string = [self.barButton.titleName.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
    NSString *url = [KbatchURL stringByAppendingString:string];
    AFHTTPSessionManager *AFManager = [AFHTTPSessionManager manager];
    AFManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain", nil];
    [AFManager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        [self.scienceArray removeAllObjects];
        [self.artsArray removeAllObjects];
        NSDictionary *outDictionary = [NSDictionary dictionaryWithDictionary:responseObject];
        NSArray *liArrray = outDictionary[@"li_pici"];
        NSArray *wenArray = outDictionary[@"wen_pici"];
        for (NSDictionary *dict in liArrray) {
            GKZYBatchQueryModel *batchModel = [[GKZYBatchQueryModel alloc]init];
            [batchModel setValuesForKeysWithDictionary:dict];
            [self.scienceArray addObject:batchModel];
        }
        
        for (NSDictionary *dict in wenArray) {
            GKZYBatchQueryModel *batchModel = [[GKZYBatchQueryModel alloc]init];
            [batchModel setValuesForKeysWithDictionary:dict];
            [self.artsArray addObject:batchModel];
        }
        
        [self.batchView reloadData];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"各省批次查询";
    self.leftBarButton = [[GKZYSortLeftBarButtonItemView alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc]initWithCustomView:self.leftBarButton];
    self.navigationItem.leftBarButtonItem = backBar;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backToMain)];
    [self.leftBarButton addGestureRecognizer:tap];
    
    self.barButton = [[GKZYBatchQueryBarButtonItemView alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];
    [self.barButton setOrangeDirectionWithTitileName:@"北京市"];
    UIBarButtonItem *rightBBI = [[UIBarButtonItem alloc]initWithCustomView:self.barButton];
    self.navigationItem.rightBarButtonItem = rightBBI;
    UITapGestureRecognizer *tap0 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(creatDropDownView)];
    [self.barButton addGestureRecognizer:tap0];
    
    [self makeData];
}


-(void)backToMain{
    [self.navigationController popViewControllerAnimated:YES];
}

//右buttonItem点击事件
-(void)creatDropDownView{
    //切换popview的显现状态
    self.popView.DropDown = !self.popView.DropDown;
    [self viewWillAppear:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if ([tableView isKindOfClass:[GKZYBatchTableView class]]) {
        return 1;
    }else{
        return self.provinceArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView isKindOfClass:[GKZYBatchTableView class]]) {
        GKZYBatchQueryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[GKZYBatchQueryCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        }
        [cell configureTheCellWithScienceArray:self.scienceArray andArtsArray:self.artsArray];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"popCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"popCell"];
        }
        cell.textLabel.text = self.provinceArray[indexPath.row];
        return cell;
    }
   }




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView isKindOfClass:[GKZYBatchPopView class]]) {
        return 40;
    }else{
       
        return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height ;
     }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView isKindOfClass:[GKZYBatchPopView class]]) {
        self.barButton.titleName.text = self.provinceArray[indexPath.row];
        self.popView.DropDown = NO;
        [self viewWillAppear:YES];
        [self requestTheData];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.01;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
